package com.yipinhouse.backstage.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.yipinhouse.backstage.client.BrokerInfoWebManageClient;
import com.yipinhouse.backstage.client.param.AddBrokerWebParam;
import com.yipinhouse.backstage.client.param.OrderHouseWebParam;
import com.yipinhouse.backstage.service.ShopService;
import com.yipinhouse.backstage.service.dto.ShopDto;
import com.yipinhouse.backstage.service.param.PromotionServiceParam;
import com.yipinhouse.common.result.Result;
import com.yipinhouse.employee.client.BrokerInfoManageClient;
import com.yipinhouse.employee.client.BrokerInfoQueryClient;
import com.yipinhouse.employee.client.param.AddBrokerParam;
import com.yipinhouse.employee.client.param.OrderHouseParam;
import com.yipinhouse.employee.client.result.PromotionResultInfo;
import com.yipinhouse.employee.client.result.ShopBrokerInfoResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("backstage")
public class BrokerInfoWebManageApiImpl implements BrokerInfoWebManageClient {

    @Resource
    private ShopService shopService;

    @Resource
    private BrokerInfoManageClient brokerInfoManageClient;

    @Resource
    private BrokerInfoQueryClient brokerInfoQueryClient;

    /**
     * 关联员工与门店
     *
     * @param shopId
     * @param brokerId
     * @return
     */
    @Override
    public Result correlation(Integer shopId, Integer brokerId) {
        ShopDto shopDto = shopService.checkShop(shopId);
        log.info("门店信息：{}", shopDto);
        brokerInfoManageClient.correlation(shopId, brokerId);
        return Result.getInstance().setMsg("关联成功").setCode(200);
    }

    /**
     * 将员工升职为店长
     *
     * @param brokerId
     * @return
     */
    @Override
    public Result promotion(Integer brokerId, Integer shopId) {
        //查看该门店是否已有店长
        shopService.checkShopowner(shopId);
        //获取员工信息
        Result<PromotionResultInfo> promotion = brokerInfoQueryClient.promotion(brokerId, shopId);
        PromotionServiceParam param = BeanUtil.copyProperties(promotion.getData(), PromotionServiceParam.class);
        shopService.promotion(param, shopId);
        return Result.getInstance().setMsg("升职成功").setCode(200);
    }

    @Override
    public Result addBroker(AddBrokerWebParam param) {
        AddBrokerParam addBrokerParam = BeanUtil.copyProperties(param, AddBrokerParam.class);
        Result result = brokerInfoManageClient.addBroker(addBrokerParam);
        return result;
    }

    @Override
    public Result orderHouse(OrderHouseWebParam param) {
        OrderHouseParam orderHouseParam = BeanUtil.copyProperties(param, OrderHouseParam.class);
        Result result = brokerInfoManageClient.orderHouse(orderHouseParam);
        return result;
    }

    @Override
    public Result<List<ShopBrokerInfoResult>> getBrokerByShopId(Integer shopId) {
        Result<List<ShopBrokerInfoResult>> brokerByShopId = brokerInfoManageClient.getBrokerByShopId(shopId);
        return brokerByShopId;
    }
}
